import os,requests
from urllib.parse import urlencode
from urllib.request import urlretrieve

#全局变量，用于生成文件夹
count=1

def getJson(offset):
        '''获取网页代码'''
        params = {
                'aid': '24',
                'app_name': 'web_search',
                'offset': offset,
                'format': 'json',
                'keyword': '街拍',
                'autoload': 'true',
                'count': '20',
                'en_qc': '1',
                'cur_tab': '1',
                'from': 'search_tab',
                'pd': 'synthesis',
                'timestamp': '1596029869734',
                '_signature': 'fc2IUgAgEBC7mjHfsIwKrn3MyUAACLvErwt5hIacA-qeTz2rYdntF-sFxoVqthyyZ3i2BDXXgXpLQ5h9iLC-HoOEa46AUPkhAlWZZGrnFFqBO9sDX.bSC3BCImv7yCHP4yF'
         }
  
        url ='https://www.toutiao.com/api/search/content/?' + urlencode(params)
  
        res = requests.get(url)

        if res.status_code == 200:
            return res.json()
        else:
            print("error")
  
def getImage(json):
    '''得到代码中的图片和图片名'''
    data = json.get("data")
    for item in data:
        image_list = item.get("image_list",[])
        title = item.get("title")
        for im in image_list:
            yield{
                "image":im.get("url"),
                "title":title
                }
      
def saveImage(i):
    '''保存图片'''
    global count
    path = os.path.join("./pic",str(count))
    print(path)
    #print(i.get('title'))
    if not os.path.exists(path):
            os.mkdir(path)
  
    local_image_url = i.get("image")
 
    image_url = local_image_url.replace("list/190x124","large")
    name = path+"/"+local_image_url.split("/").pop()+".jpg"
    #print(image_url)
    #print(name)
     
    urlretrieve(image_url,name)
    count += 1
  
  
def main(offset):
    json = getJson(offset)
    print(json)
    for i in getImage(json):
        saveImage(i)
         
  
if __name__ == "__main__":
    main(0)
